﻿var editor;
var popup;
var formats = "css txt js sql java cs html cshtml config ";
var path;

$('.filelink').click(function () {

    var ext = $(this).text().split('.');
    ext = ext[ext.length - 1];

    var re = new RegExp(ext);

    if (formats.match(re) != null) {

        editor = ace.edit("editarea");


        switch (ext){
            case "css ":               
                editor.getSession().setMode("ace/mode/css");
                break;
            case "js ":
                editor.getSession().setMode("ace/mode/javascript");
                break;
            case "java ":
                editor.getSession().setMode("ace/mode/java");
                break;
            case "cs ":
                editor.getSession().setMode("ace/mode/csharp");
                break;
            case "sql ":
                editor.getSession().setMode("ace/mode/sql");
                break;
            case "html":
                editor.getSession().setMode("ace/mode/html");
                break;
            case "xml":
                editor.getSession().setMode("ace/mode/xml");
                break;
            case "config":
                editor.getSession().setMode("ace/mode/xml");
                break;
                
        }

        popup = $('#editorpopup').bPopup({

            easing: 'easeOutBack',
            speed: 500,
            transition: 'slideDown',
            onClose: function () {
                editor.destroy();
            }
        });


        $.ajax({
            url: "/Edit/LoadFile",
            data: { id: $(this).attr('href') },
            type: "POST",
            dataType: "json",
            success: function (data) {
                $('#loading').hide();
                editor.setValue(data.cont);
                path = data.path;
            }
        });


    }


});

function closeEditor() {
    popup.close();
}

function saveEditor() {
    var cont = new String(editor.getValue());

    $.ajax({
        url: "Edit/SaveFile",
        data: JSON.stringify({ content: editor.getValue(), path: path }),
        type: "POST",
        dataType: "json",
        contentType: 'application/json',
        success: function (data) {
            $('#editorpopup').prepend('<span class="wassaved" style="position:absolute;margin-top:40px;margin-left:900px;z-index:9999;color:#ffb768;font-size:12px">Saved</span>');
            setTimeout(function () {
                $('.wassaved').remove();
            }, 2000);
        },
        processData: true
    });
}

//function expandEditor() {

//    $('#editarea').animate({        
//        'margin-left': '-' + (window.innerWidth - $('#editarea').width()) / 2 + 'px',
//        'margin-top': '-' + (window.innerHeight-200 - $('#editarea').height()) / 2 + 'px'
//    });

//    $('#editnav').animate({
//        'margin-left': '-' + (window.innerWidth - $('#editarea').width()) / 2 + 'px',
//        'margin-top': '-' + (window.innerHeight - 200 - $('#editarea').height()) / 2 + 'px'
//    });

//}


//-----------------------------------------------------------------------------------------------------------fileInfo

$('.filelink, .folderlink').mouseenter(function () {

    if ($(this).attr('name') !== null) {


        var info = $(this).attr('name').split(',');

        var divHtml = '<div class="fileinfo"></div>';

        $('#main').prepend(divHtml);

        var div = $('.fileinfo');

        if ($(this).hasClass('filelink')) {
            var sizeKB = parseInt(info[1]) / 1024;

            if (sizeKB > 1024) {
                var sizeMB = sizeKB / 1024;
                info[1] = sizeMB.toFixed(2) + " MB";
            }
            else {
                info[1] = sizeKB.toFixed(2) + " KB";
            }
            
            div.append('<text><b>Name: <b></text>' + '<span>' + info[0] + '</span><br>');
            div.append('<text><b>Size: <b></text>' + '<span>' + info[1] + '</span><br>');
            div.append('<text><b>Owner: <b></text>' + '<span>' + info[3] + '</span><br>');
            div.append('<text><b>Creation date: <b></text>' + '<span>' + info[4] + '</span><br>');
            div.append('<text><b>Last update: <b></text>' + '<span>' + info[5] + '</span><br>');
        }
        else {
            
            div.append('<text><b>Name: <b></text>' + '<span>' + info[0] + '</span><br>');            
            div.append('<text><b>Owner: <b></text>' + '<span>' + info[1] + '</span><br>');
            div.append('<text><b>Creation date: <b></text>' + '<span>' + info[2] + '</span><br>');
            div.append('<text><b>Last update: <b></text>' + '<span>' + info[3] + '</span><br>');
        }
        var x;
        var y;

        $('.filelink, .folderlink').mousemove(function (event) {

            x = event.pageX;
            y = event.pageY;

            div.css({
                'margin-left': x -220 + 'px',
                'margin-top': y - 50 + 'px'
            });

                        
            div.hide().delay(1000);
            
            div.fadeIn();

            
        });

        $('.filelink, .folderlink').mouseout(function () {
            $('.fileinfo').remove();

        });
        
    }
          

});



